<template>
  <el-form :model="nodeModalForm" ref="nodeModal" label-placement="left" label-width="120px" :rules="nodeModalRules"
           size="small">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model.trim="nodeModalForm.nodeName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-table :data="rowsList" border ref="selectTableRef" class="w_100" height="400"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column align="center" type="index" width="50" label="序号" />
          <el-table-column align="center" property="COLUMN_NAME" label="字段名称" />
          <el-table-column align="center" property="DATA_TYPE" label="字段类型" />
          <el-table-column align="center" property="COLUMN_COMMENT" label="字段注释" />
          <el-table-column align="center" width="150" label="别名" property="alias"></el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <div class="nodeModal-footer">
      <span class="dialog-footer">
        <el-button plain @click="handleConfigCancel">取消</el-button>
        <el-button type="primary" @click="handleConfigConfirm">更新</el-button>
      </span>
    </div>
  </el-form>
</template>

<script>
import { computed, defineComponent, inject, nextTick, onMounted, ref } from "vue";
import { separatorList } from "../../../../../utils/dict";
import { useEtlStore } from "../../../../../store/flowEtl"

export default defineComponent({
  name: "node-config-select",
  props: {
    node: Object,
  },
  components: {},
  setup (props, ctx) {
    const etlStore = useEtlStore();
    const selectTableRef = ref();
    const nodeList = computed(() => etlStore.nodeList);
    const rowsList = ref([]);
    const nodeConfigClose = inject("nodeConfigClose");
    const nodeModalForm = ref({
      nodeName: "",
      fields: [],
    });
    const nodeModal = ref();
    const nodeModalRules = ref({
      nodeName: {
        required: true,
        message: "请输入数据源名称",
        trigger: "blur",
      },
    });
    const handleConfigCancel = () => {
      nodeConfigClose();
    };
    const nodeConfigUpdate = (node) => {
      let nodes = [...nodeList.value].map(item => {
        if (item.id === node.id) {
          item.status = true;
          item.nodeName = node.nodeName;
          item.config.fields = node.fields;
        }
        return item;
      });
      etlStore.setNodeList(nodes);
      nodeConfigClose();
    };
    const handleConfigConfirm = () => {
      nodeModal.value.validate((valid) => {
        if (valid) {
          nodeConfigUpdate(nodeModalForm.value);
        } else {
          return false;
        }
      });
    };
    const resetForm = () => {
      nodeModalForm.value = {
        id: null,
        nodeName: "",
        fields: [],
      };
    };
    const handleSelectionChange = (val) => {
      nodeModalForm.value.fields = val
    }
    onMounted(() => {
      resetForm();
      nodeModalForm.value = {
        id: props.node.id,
        nodeName: props.node.nodeName,
        fields: props.node.config.fields,
      };
      rowsList.value = props.node.originFields;
      rowsList.value.forEach(item => {
        nodeModalForm.value.fields.forEach(field => {
          if (field.alias === item.alias) {
            nextTick(() => {
              selectTableRef.value.toggleRowSelection(item, true)
            })
          }
        })
      })
    });
    return {
      separatorList: separatorList,
      rowsList,
      nodeList,
      nodeModalForm,
      nodeModalRules,
      nodeModal,
      selectTableRef,
      resetForm,
      handleConfigCancel,
      handleConfigConfirm,
      nodeConfigUpdate,
      handleSelectionChange,
      nodeConfigClose,
    };
  },
});
</script>

<style scoped>
.field-table {
  padding: 0 12px 24px;
}

.nodeModal-footer {
  padding: var(--el-dialog-padding-primary);
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
}
</style>